<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

    // 应用公共文件
    use think\Db;

    /**
     * 判断是否为空
     */
    function is_null_or_empty($value)
    {
        if (is_null($value) || $value === "") {
            return true;
        }
        return false;
    }

    /**
     * 生成订单号
     * @return string
     */
    function build_order_no()
    {
        return substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 5);
    }

/**
 * 生成订单号
 * @return string
 */
function build_order_no1()
{
    return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}

    /**
     * 判断手机号格式
     */
    function is_mobile($mobile)
    {
        if (! is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }

    /**
     * Db方法
     */
    function D($tablename)
    {
        return Db::name($tablename);
    }

    /**
     * I方法
     */
    function I($param)
    {
        return trim(input($param));
    }

    /**
     * 32位uuid
     */
    function uuidBy32()
    {
        $charid = md5(uniqid(mt_rand(), true));
        $hyphen = chr(45); // "-"
        $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12);
        return str_replace('-', '', $uuid);
    }

    /**
     * post进行url请求
     */
    function request_post($url = '', $post = array())
    {
        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $post
        );
        $ch = curl_init($url);
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($result, true);
        return $res;
    }

    /**
     * get进行url请求
     */
    function request_get($url)
    {
        $result = file_get_contents($url);
        $res = json_decode($result, true);
        return $res;
    }

    /**
     * 获取毫秒级时间戳
     * @return float
     */
    function getMillisecond()
    {
        list ($s1, $s2) = explode(' ', microtime());
        return (float) sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    }


    /**
     * 保存运行日志
     */
    function saveLog($content, $filename = NULL)
    {
        if ($filename == null) {
            $filename = date('Y-m-d', time());
        }
        $path = 'ZLog/';
        $chmod = 777;
        if (! file_exists($path)) {
            @mkdir($path);
            @chmod($path, $chmod);
        }
        $file = 'ZLog/' . $filename . '.txt';
        @file_put_contents($file, date('Y-m-d H:i:s', time()) . $content . "\n\r", FILE_APPEND);
    }

    /**
     * 获取当前时间戳
     * @return false|string
     */
    function getnowtime()
    {
        return date("YmdHis");
    }

    /**
     * 递归删除文件夹
     * @param $dirName
     */
    function delFileUnderDir($dirName)
    {

        if (! is_dir($dirName)) {
            mkdir($dirName, 0777, true);
        }


        if ($handle = opendir("$dirName")) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..") {
                    if (is_dir("$dirName/$item")) {
                        //delFileUnderDir("$dirName/$item");
                    } else {
                        if (unlink("$dirName/$item")) {
                            // echo "成功删除文件： $dirName/$item\n";
                        }
                    }
                }
            }
            closedir($handle);
        }
    }

    /**
     * 二维数组排序
     */
    function array_sort($arrs, $key, $orderby = false)
    {
        if (count($arrs)<2){
            return $arrs;
        }
        if (!$orderby) {
            $orderby = "SORT_DESC";
        }else {
            $orderby = "SORT_ASC";
        }
        $sort = array(
            'direction' => $orderby, // 排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
            'field' => $key
        );
        $arrSort = array();
        foreach ($arrs as $uniqid => $row) {
            foreach ($row as $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if ($sort['direction']) {
            array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arrs);
        }
        return $arrs;
    }

    function str_get_last($str,$n)
    {
        return substr($str,strlen($str)- $n,1); //总长度减3，得到的长度往后截取3位，得到的就是最后三位
    }